package com.ymatou.iapi.optrade.service;

import java.math.BigDecimal;
import java.util.Arrays;

import com.google.common.collect.Lists;
import com.ymatou.iapi.optrade.parameter.enums.BizTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.OrderBizReq;
import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.vo.BizVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 
 * 砍价团服务
 *
 * bizSubType  业务子类型; 砍价团(1 开团, 2 参团)
 * 
 * 业务状态 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)
 * 
 */
public class BargainGroupService {
	
	private static Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
	private static String sellerName = String.valueOf(EnvSetup.getData("autotest_sellerName1"));
	
	/**
	 * 开团
	 * 
	 * @param bizId 团id
	 *          
	 * */
	public static TradingServiceResp openGroup(String bizId) {

		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		BizVo bizVo = new BizVo();

		bizVo.setBizId(bizId);
		bizVo.setBizSubType(1);
		bizVo.setBizStatus(0);
		
		OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
				TestDataManager.getRandomUUID().toString(), TestDataManager
						.getRandomUUID().toString());
		
		orderVo.setOrderItems(Arrays.asList(orderItemVo));
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));
		placeOrderReq.setBizSource(bizVo);

		TradingServiceResp placeOrderResp = TradingCallService
				.callPlaceOrderService(placeOrderReq);

		long tradingId = placeOrderResp.getTradingId();
		BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();
		long userId = placeOrderResp.getUserId();

		// 支付订单
		TradingCallService.payOrder(userId, useAccountAmount, tradingId, "13");

		return placeOrderResp;
	}
	
	/**
	 * 参团
	 * 
	 * */
	public static TradingServiceResp joinGroup(String bizId) {

		//砍价团参团
		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		BizVo bizVo = new BizVo();

		bizVo.setBizId(bizId);
		bizVo.setBizSubType(2);
		bizVo.setBizStatus(0);
		
		OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
				TestDataManager.getRandomUUID().toString(), TestDataManager
						.getRandomUUID().toString());
		
		orderVo.setOrderItems(Arrays.asList(orderItemVo));
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));
		placeOrderReq.setBizSource(bizVo);

		TradingServiceResp placeOrderResp = TradingCallService
				.callPlaceOrderService(placeOrderReq);

		long tradingId = placeOrderResp.getTradingId();
		BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();
		long userId = placeOrderResp.getUserId();

		// 支付订单
		TradingCallService.payOrder(userId, useAccountAmount, tradingId, "13");

		return placeOrderResp;
	}
	
	/**
	 * 创建一个虚拟商品订单
	 * 
	 * */
	public static TradingServiceResp placeVirtualProduct() {

		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		BizVo bizVo = new BizVo();

		// 虚拟商品
		int bizType = BizTypeEnum.VIRTUAL_PRODUCT.getCode();

		String bizId = TestDataManager.getRandomNum(5);

		bizVo.setBizId(bizId);
		bizVo.setBizType(bizType);
		
		OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
				TestDataManager.getRandomUUID().toString(), TestDataManager
						.getRandomUUID().toString());
		
		orderVo.withLogisticsType(LogisticsTypeEnum.AutoDelivery)
				.setOrderItems(Arrays.asList(orderItemVo));
		
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));
		placeOrderReq.setBizSource(bizVo);	

		return TradingCallService.callPlaceOrderService(placeOrderReq);
	}

	/**
	 * 砍价团服务通知交易系统，团状态接口
	 * 
	 * @param bizId
	 *            团id
	 * @param orderId
	 *            订单号
	 * @param bizSubType
	 *            砍价团(1 开团, 2 参团)
	 * @param bizStatus
	 *            状态 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3
	 *            组团成功)
	 * */
	public static void notifyOrderBizStatus(String bizId, long orderId,
			int bizSubType, int bizStatus) {

		OrderBizReq orderBizReq = new OrderBizReq();
		ChangeOrderBizStatusCall changeorderbizstatusCall = new ChangeOrderBizStatusCall();

		try {
			BizVo bizVo = new BizVo();

			bizVo.setBizStatus(bizStatus);
			bizVo.setBizSubType(bizSubType);
			bizVo.setBizType(1);
			bizVo.setOrderId(orderId);
			bizVo.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo));

			changeorderbizstatusCall.setData(false, orderBizReq);
			changeorderbizstatusCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) {
		
		BargainGroupService.notifyOrderBizStatus("418027411", 7760548699L, 2, 3);
	}
}
